#ifndef SEQUENCE_FILTER
#define SEQUENCE_FILTER

#define MAKE_SEQFILTER_MED(name,type,size) \
        static type name##Flt[size] = {0}; \
        type name##Get(){ return name##Flt[size>>1]; }\
        void name##Set(type val){\
            for( int i=0; i<size; ++i ){\
                if( val < name##Flt[i] ){\
                    for( int j=size-1; j>i; --j ){ name##Flt[j] = name##Flt[j-1]; }\
                    name##Flt[i] = val; \
                    break;\
                }else if( val > name##Flt[size-1-i] ){\
                    for( int j=0; j<size-1-i; ++j ){ name##Flt[j] = name##Flt[j+1]; }\
                    name##Flt[size-1-i] = val; \
                    break;\
                }\
            }\
        }
        
#define MAKE_SEQFILTER_AVR(name,type,size) \
        static type name##Flt[size] = {0}; \
        static double name##FltSum = 0; \
        static double name##FltRat = 1.0/size;\
        type name##Get(){ return name##FltSum*name##FltRat; }\
        void name##Set(type val){\
            double lastval =  name##Flt[size-1];\
            for( int i=1; i<size; ++i ){ name##Flt[i] = name##Flt[i-1]; }\
            name##Flt[0] = val;\
            name##FltSum += ((double)(val)-lastval);\
        }
#endif
